package com.qf.flash.ui.circle;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.model.LifeDetailsBean;
import com.qf.flash.mvp.contract.LifeDetailsContract;
import com.qf.flash.mvp.presenter.LifeDetailsPresenter;
import com.qf.flash.utils.ImageLoader;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.base.WebViewFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FunDetailsActivity extends BaseActivity implements LifeDetailsContract.View {
    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.sex)
    ImageView sex;
    @BindView(R.id.tv_day)
    TextView tvDay;
    private LifeDetailsPresenter lifeDetailsPresenter;
    private WebViewFragment mFragment;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_fun_details;
    }

    @Override
    protected void initViewAndData() {
        String id = getIntent().getStringExtra("id");
        String title = getIntent().getStringExtra("title");
        tvTopTitle.setText(title);
        lifeDetailsPresenter.getLifeDetails(id);
    }

    @Override
    public int getFragmentGroupId() {
        return R.id.fl_container;
    }

    @OnClick(R.id.tv_top_back)
    public void onViewClicked()
    {
        if (mFragment != null)
        {
            mFragment.onKeyDown(KeyEvent.KEYCODE_BACK);
        }
        else
        {
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (mFragment != null)
            return mFragment.onKeyDown(keyCode);
        return super.onKeyDown(keyCode, event);
    }

    public static void start(Context context, String id, String type) {
        Intent intent = new Intent(context, FunDetailsActivity.class);
        intent.putExtra("id", id);
        if (type.equals("0")) {
            intent.putExtra("title", "搞笑");
        } else if (type.equals("1")) {
            intent.putExtra("title", "生活");
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void showDatas(LifeDetailsBean lifeDetailsBean) {
        ImageLoader.loadCircle(this,lifeDetailsBean.getPhoto(),ivHead);
        tvName.setText(lifeDetailsBean.getUserName());
        String sexType = lifeDetailsBean.getSex();
        if(sexType.equals("1")){
            sex.setImageResource(R.mipmap.icon_man);
        }else{
            sex.setImageResource(R.mipmap.icon_woman);
        }
        tvDay.setText(lifeDetailsBean.getShowTime());
        if (!TextUtils.isEmpty(lifeDetailsBean.getContent()))
            mFragment = WebViewFragment.initWebViewFragment(this, lifeDetailsBean.getContent(),false);

    }

    @Override
    protected void initInject() {
        super.initInject();
        lifeDetailsPresenter = new LifeDetailsPresenter();
        initPresenters(lifeDetailsPresenter);
    }
}
